#include "Projectile.h"
#include <GL/glut.h>
#include "BMPLoader.h"
#include <vector>
#include "Collidable.h"
#include "Particle.h"
#include "Enemy.h"
#include <iostream>

using namespace std;

const int Projectile::LIFETIME = 100;
const int Projectile::DROP_DELAY = 20;

Projectile::Projectile() : hasCollided(false), dead(false), age(0), dropTime(DROP_DELAY)
{
	BMPClass bmp;
	BMPLoad("big_glow.bmp",bmp);
	
	glGenTextures(1, &glowTexture);
	glBindTexture(GL_TEXTURE_2D, glowTexture);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
	
	glTexImage2D(GL_TEXTURE_2D,0,3,bmp.width,bmp.height,0,GL_RGB,GL_UNSIGNED_BYTE,bmp.bytes);
}

void Projectile::move(const std::vector<Collidable*>& plats)
{
	if ( !hasCollided)
	{
		if ( age > LIFETIME )
		{
			//dead = true;
			hasCollided = true;
			return;
		}else{
			age++;
		}

		if (dropTime >= DROP_DELAY)
		{
			Particle* p = new Particle();
			p->setPosition(position.x,position.y, 0);
			//p->xVel = xVel;
			p->setScale(40,40,40);
			particleList.push_back(p);
			dropTime = 0;
		}else{
			dropTime++;
		}
	position.x += xVel;
	Collidable* collision = checkCollision(plats);
	if (collision){

		if (collision->type == ENEMY)
		{
			Enemy* e = dynamic_cast<Enemy*>(collision);
			e->youreDead(xVel);
		}
		
		hasCollided = true;
		position.x -= xVel;
		xVel = 0;

		// SPAWN PARTICLES
		//int numParts = rand() % (Particle::MAX_COUNT - Particle::MIN_COUNT + 1) + Particle::MIN_COUNT;
		int numParts = 20;
		
		for (int i = 0; i < numParts; i++)
		{

			Particle* p = new Particle();
			p->setPosition(position.x,position.y, 0);
			float xFrac = rand() % 200 - 100;
			xFrac = xFrac / 100.f;

			p->xVel = Particle::SPEED * xFrac;
			xFrac = rand() % 200 - 100;
			xFrac = xFrac / 100.f;
			p->yVel = Particle::SPEED * (1 - xFrac);
			
			p->setScale(40,40,40);
			particleList.push_back(p);
			
			
		}
	}
	}

	// MOVE PARTICLES
		for (std::vector<Particle*>::iterator it = particleList.begin(); it != particleList.end();)
		{
			if ( (*it)->dead )
			{
				//dead = true;
				delete (*it);
				it = particleList.erase(it);
			}else{
				(*it)->move(plats);
				it++;
			}
		}

		if (particleList.empty() && hasCollided)
			dead = true;

}

void Projectile::draw()
{
	if ( !hasCollided )
	{

	// DRAW PROJECTILE GLOW	
	glColor4f(0.8,0.8,1,1);
	glDisable(GL_LIGHTING);
	glEnable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
	glTexEnvi(GL_TEXTURE_ENV, 
			GL_TEXTURE_ENV_MODE, GL_MODULATE); 
	
	glBindTexture(GL_TEXTURE_2D, glowTexture);
	glBegin(GL_QUADS);
		glTexCoord2f(1.0,1.0);
		glNormal3f(0.0f, 0.0f, 0.0f);
		glVertex3f(position.x + 150, position.y + 150, 0);
		glTexCoord2f(0.0,1.0);
		//glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(position.x - 150, position.y + 150, 0);
		glTexCoord2f(0.0,0.0);
		//glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(position.x - 150, position.y - 150, 0);
		glTexCoord2f(1.0,0.0);
		//glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(position.x + 150, position.y - 150, 0);
	glEnd();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);

	

	// DRAW PROJECTILE
	
	//glColor4f(0.8,0.8,1,1);
	//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glTranslatef(position.x,position.y,0);
	glutSolidSphere(50,20,20);
	glPopMatrix();

	glEnable(GL_LIGHTING);
	//glDisable(GL_BLEND);
	
	}

	// DRAW PARTICLES

		for (std::vector<Particle*>::iterator it = particleList.begin(); it != particleList.end(); it++)
		{
			(*it)->draw();
		}
	
	
}

Projectile::~Projectile()
{
	for (std::vector<Particle*>::iterator it = particleList.begin(); it != particleList.end(); it++)
	{
		delete (*it);
	}
}